
local desc_lieding = [[
  # 裂鼎模式简介

  ___

  裂鼎是三国杀1V1模式，颠覆了三国杀原版的体系机制玩法。

  裂鼎一共有三个牌堆，分别是指令区、计谋区、锻造区，每个牌堆各有52张牌。

  - **指令区**：真正意义上的牌堆，其他只能算是公共资源集中点。大家摸牌、判定用到的牌堆都是“指令区”，里面一共有八种指令牌，相当于三国杀的基本牌。

  - **计谋区**：里面都是计谋牌，也就是三国杀的锦囊牌。计谋牌分成了四大类，分别是拆卸类、伤害类、控制类、增益类，每种花色代表一类，每类13张。

  - **锻造区**：里面全是装备牌，根据类别分为武器牌、防具牌、坐骑牌、宝物牌。类别的分布不会和计谋区一样固定各四分之一，而是考虑游戏节奏，武器牌占比最多，其次是防具、坐骑、宝物。

  *指令系统是控制裂鼎游戏节奏的一大核心机制，随着回合数的增长，玩家的指令上限越来越多，能做的事也越来越多，节奏逐渐加速。*

  具体见https://tieba.baidu.com/p/8735507456?pn=1
]]

local lieding_rules =
  "<br />1.点击角色的区域标记查看方案区、备用区和剩余指令、指令上限。"..
  "<br />2.计谋牌默认放在方案区，不计入手牌上限。方案区最多三张牌，超过的话需要顶掉一张。"..
  "<br />3.指令上限每回合增加2点，最多加到8点，每回合开始时将指令补至上限。"..
  "<br />4.前期最好以发育找装备为主，不要盲目吃粮；没指令了回合外就不能出牌了，要注意留指令。"..
  "<br />5.裂鼎只有一个坐骑栏，备用标签的装备放在备用栏内，和普通装备一样生效。每人有两个备用栏。"..
  "<br />6.方针效果持续5回合，5回合后自动弃置。"

local ld = require "packages/brokenroyal/util"
local U = require "packages/utility/utility"

local brokenroyal_getLogic = function()
  local brokenroyal_logic = GameLogic:subclass("brokenroyal_logic")

  function brokenroyal_logic:assignRoles()
    local room = self.room
    local n = #room.players
    local roles ={ "lord", "rebel" }
    for i = 1, n do
      local p = room.players[i]
      p.role = roles[i]
      room:setPlayerProperty(p, "role_shown", true)
      room:broadcastProperty(p, "role")
    end
    self.start_role = roles[1]
    -- for adjustSeats
    room.players[1].role = "lord"
  end

  function brokenroyal_logic:chooseGenerals()
    local room = self.room
    local generalNum = room.settings.generalNum
    local lord = room:getLord()
    room.current = lord
    lord.role = self.start_role
    local nonlord = room.players
    local generals = Fk:getGeneralsRandomly(#nonlord * generalNum)
    table.shuffle(generals)
    for _, p in ipairs(nonlord) do
      local arg = {}
      for i = 1, generalNum do
      table.insert(arg, table.remove(generals, 1).name)
      end
      p.request_data = json.encode({ arg, 1 })
      p.default_reply = arg[1]
    end
    room:doBroadcastRequest("AskForGeneral", nonlord)
    for _, p in ipairs(nonlord) do
      if p.general == "" and p.reply_ready then
        local general = json.decode(p.client_reply)[1]
        room:setPlayerGeneral(p, general, true, true)
      else
        room:setPlayerGeneral(p, p.default_reply, true, true)
      end
      p.default_reply = ""
    end
    room:broadcastProperty(lord, "general")
    room:broadcastProperty(lord, "kingdom")
  end

  
  function brokenroyal_logic:prepareDrawPile()
    local room = self.room
    local seed = math.random(2 << 32 - 1)
    local allCardIds = Fk:getAllCardIds()
    local tricks = {}
    local equips = {}
    for i = #allCardIds, 1, -1 do
      local card = Fk:getCardById(allCardIds[i])
      --if card.is_derived then
      if card.type == Card.TypeTrick or card.type == Card.TypeEquip then
        local id = allCardIds[i]
        table.remove(allCardIds, i)
        --table.insert(room.void, id)
        if card.type == Card.TypeTrick then
          table.insert(tricks, id)
        elseif card.type == Card.TypeEquip then
          table.insert(equips, id)
        end
        room:setCardArea(id, Card.Void, nil)
      end
      if table.contains(ld.policy, card) then
        room:setCardMark(card, "@@policy", 1)
      elseif table.contains(ld.trap, card) then
        room:setCardMark(card, "@@trap", 1)
      elseif table.contains(ld.standby, card) then
        room:setCardMark(card, "@@standby", 1)
      end
    end
    table.shuffle(tricks)
    table.shuffle(equips)
    room:setBanner("@$tricks", tricks)
    room:setBanner("@$equips", equips)
    table.shuffle(allCardIds, seed)
    room.draw_pile = allCardIds
    for _, id in ipairs(room.draw_pile) do
      room:setCardArea(id, Card.DrawPile, nil)
    end
    room:doBroadcastNotify("PrepareDrawPile", seed)
  end

  return brokenroyal_logic
end

local brokenroyal_rule = fk.CreateTriggerSkill{
  name = "#brokenroyal_rule",
  priority = 0.001,
  refresh_events = {fk.GameStart, fk.DrawInitialCards, fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    return event == fk.GameStart or target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      room:handleAddLoseSkills(player, "#noinstruction|#card_return_skill|DiscoverSkill&|#use_standby|#ld_prohibit_move|#max_plan|#policytrap_instructions|#scenario_visible", nil, false)
      room:handleAddLoseSkills(player, "#policy_skill|#trap_skill|#plan_trigger|#rumble_trigger|#grain_trigger|#upraising_buff|#coercion_buff|#encyclopedia_buff|#armistice_buff"..
      "|#rockfall_trigger|#ambush_trigger|#empoison_trigger|#spreadrumor_trigger|#honeytrap_trigger|#mislead_trigger|#neglectarmy_trigger|#enticeleave_clear|#borrowarrows_trigger"..
      "|#dismantlement_trigger|#dismantlement_prohibit|#recall_trigger|#lure_enemy_trigger|#gropefish_trigger|#br__snatch_trigger|#speedprecious_trigger", nil, false)
      room:handleAddLoseSkills(player, "#brokenroyal_targetmod|#haidongqing_show|#boomerang_slash|#bamboojavelin_slash|#iron_tribulus|#ld_breastplate"..
      "|#chain_mail|#demolisher|#armillary_skill|#camel_trigger|#ferghana_damage|#bronco_damage|#nightmare_damage|#poster_draw|#warelephant_throw"..
      "|#whitehorse_draw|#chariot_slash|#goodhorse_recover", nil, false)
      room:setPlayerMark(player, "maxinstruction", 2)
      ld.addinstruction(player, 2)
      room:setTag("rumble_effected", false)
      room:setBanner("@[:]tips", "brokenroyal_tips")
      if player.role ~= "lord" then
        --local card = room:getCardsFromPileByRule("plan", 1)
        local card = room:printCard("plan", Card.NoSuit, 0)
        room:addCardMark(card, "__destr_discard")
        room:addCardMark(card, "@@coin-inhand")
        room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, self.name, "", true)
      end
    elseif event == fk.DrawInitialCards then
      if player.seat == 1 then
        data.num = data.num - 1
      end
    else
      local maxn = 8
      if player:getMark("guixin_buff") > 0 then maxn = 10 end
      if player.room:getTag("RoundCount") > 1 and player:getMark("maxinstruction") < maxn then
        room:addPlayerMark(player, "maxinstruction", 2)
      end
      local n = player:getMark("maxinstruction") - player:getMark("instruction")
      if n > 0 and player:getMark("@@tieqi_buff") == 0 then
        ld.addinstruction(player, n)
      end
    end
  end,
}

local max_plan = fk.CreateMaxCardsSkill{
  name = "#max_plan",
  frequency = Skill.Compulsory,
  exclude_from = function(self, player, card)
    return player:hasSkill(self) and card:getMark("@@coin-inhand") > 0
  end,
}

Fk:addSkill(max_plan)
Fk:addSkill(brokenroyal_rule)

local brokenroyal_mode = fk.CreateGameMode{
  name = "brokenroyal_mode",
  minPlayer = 2,
  maxPlayer = 2,
  rule = brokenroyal_rule,
  logic = brokenroyal_getLogic,
  surrender_func = function(self, playedTime)
    local surrenderJudge = { { text = "time limitation: 1 min", passed = playedTime >= 60 } }
    return surrenderJudge
  end,
  winner_getter = function(self, victim)
    local room = victim.room
    local alive = table.filter(room.alive_players, function(p)
      return not p.surrendered
    end)
    local winner = alive[1].role
    for _, p in ipairs(alive) do
      if p.role ~= winner then
        return ""
      end
    end
    return winner
  end
}

brokenroyal_mode.whitelist = {"ldheroes","ldcards","ldequips"}

Fk:loadTranslationTable{
  ["brokenroyal"] = "裂鼎模式",
  ["brokenroyal_mode"] = "裂鼎",

  ["time limitation: 1 min"] = "游戏时长达到1分钟",

  [":brokenroyal_mode"] = desc_lieding,

  ["@[:]tips"] = "特殊规则",
  ["brokenroyal_tips"] = "提示",
  [":brokenroyal_tips"] = lieding_rules,

  ["@instruction"] = "指令",
  ["@@coin-inhand"] = "硬币",

  ["ld_trick"] = "计谋牌",
  ["@$tricks"] = "<font color=\"#ADD8E6\">计谋区</font>",
  ["@$equips"] = "<font color=\"#93C572\">锻造区</font>",
  ["@@policy"] = "<font color=\"#93C572\">方针</font>",
  ["@@trap"] = "<font color=\"#C9A9A6\">毒计</font>",
  ["@@standby"] = "<font color=\"#FFFAA0\">备用</font>",
  ["policy"] = "<font color=\"#93C572\">方针</font>",
  ["trap"] = "<font color=\"#C9A9A6\">毒计</font>",
  ["#standby"] = "<font color=\"#DAA520\">备用区</font>",
}

return brokenroyal_mode